package cn.power.controller;

import cn.power.pojo.User;
import cn.power.service.UserService;
import cn.power.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/doLogin")
    public String login(@RequestParam String userName,
                        @RequestParam String password,
                        HttpSession session, HttpServletRequest request){
        System.out.println("controller");
        User user=userService.findUser(userName,password);
        if(user!=null){
            session.setAttribute(Constants.USER_SESSION,user);
            return "redirect:/main";
        }
        request.setAttribute("error","用户名或密码错误");
        return "/login";
    }
    @RequestMapping(value = "main")
    public String main(HttpSession session){
        if(session.getAttribute(Constants.USER_SESSION)==null){
            return "redirect:/login";
        }
        return "index";
    }
}
